﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using jTCodeGenerate.Models;

namespace jTCodeGenerate.Command
{
    public class SQLCommand
    {
        public static void AddTemplate(TemplateMdoel en)
        {
            var sql = $@"insert into SysTemplate(DisplayName,Name,RelativePath,Content,ConnectionStr,TableNameSuffix,TableNamePrefix,IgnoreTableNameSuffix,IgnoreTableNamePrefix,EntitySuffix,EntityPrefix,IgnoreEntitySuffix,IgnoreEntityPrefix,EntityGeneratePath,`Key`,IgnoreField,Alias,`Order`,FileSuffix,SolName,SolId)
values('{en.DisplayName}', '{en.Name}', '{en.RelativePath}', '', '{en.ConnectionStr}', '{en.TableNameSuffix}', '{en.TableNamePrefix}', '{en.IgnoreTableNameSuffix}', '{en.IgnoreTableNamePrefix}', '{en.EntitySuffix}', '{en.EntityPrefix}', '{en.IgnoreEntitySuffix}', '{en.IgnoreEntityPrefix}', '{en.EntityGeneratePath}','{en.Key}','{en.IgnoreField}','{en.Alias}','{en.Order}','{en.FileSuffix}','{en.SolName}','{en.SolId}');";

            SQLineHelper.ExecuteNonQuery(sql);
        }

        public static void EditTemplate(TemplateMdoel en)
        {
            var sql = $"update SysTemplate set DisplayName='{en.DisplayName}',Name='{en.Name}',RelativePath='{en.RelativePath}',Content='',ConnectionStr='{en.ConnectionStr}',TableNameSuffix='{en.TableNameSuffix}',TableNamePrefix='{en.TableNamePrefix}',IgnoreTableNameSuffix='{en.IgnoreTableNameSuffix}',IgnoreTableNamePrefix='{en.IgnoreTableNamePrefix}',EntitySuffix='{en.EntitySuffix}',EntityPrefix='{en.EntityPrefix}',IgnoreEntitySuffix='{en.IgnoreEntitySuffix}',IgnoreEntityPrefix='{en.IgnoreEntityPrefix}',EntityGeneratePath='{en.EntityGeneratePath}',`Key`='{en.Key}',IgnoreField='{en.IgnoreField}',Alias='{en.Alias}',`Order`='{en.Order}',`FileSuffix`='{en.FileSuffix}',`SolName`='{en.SolName}',`SolId`='{en.SolId}' where Id={en.Id}";
            SQLineHelper.ExecuteNonQuery(sql);
        }

        public static List<TemplateMdoel> GetTemplateAll()
        {
            var sql = "select * from SysTemplate";
            var result = SQLineHelper.SqlQuery(sql).ToList<TemplateMdoel>();
            return result;
        }

        public static void RemoveTemplate(IEnumerable<long> ids)
        {
            var sql = $"delete from SysTemplate where Id In({string.Join(",", ids)})";
            SQLineHelper.ExecuteNonQuery(sql);
        }

        public static void AddSolution(SolutionModel solutionModel)
        {
            var sql = $"insert into SysSolution(`Name`,`Remark`) values('{solutionModel.Name}','{solutionModel.Remark}')";
            SQLineHelper.ExecuteNonQuery(sql);
        }

        public static List<SolutionModel> GetSolutionAll()
        {
            var sql = $"select * from SysSolution";
            var result = SQLineHelper.SqlQuery(sql).ToList<SolutionModel>();
            return result;
        }

        public static List<TemplateMdoel> GetSolutionList(int solId)
        {
            var sql = $"select * from SysTemplate where SolId={solId}";
            var result = SQLineHelper.SqlQuery(sql).ToList<TemplateMdoel>();
            return result;
        }

        public static string GetSettingValue(string name)
        {
            var sql = $"select * from SysSetting st where Name='{name}'";
            var result = SQLineHelper.SqlQuery(sql).ToList<SysSettingModel>();
            if (result.Count <= 0)
                return string.Empty;
            return result[0].Value;
        }
    }
}
